package com.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.common.BaseContext;
import com.reggie.entity.AddressBook;
import com.reggie.mapper.AddressBookMapper;
import com.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {

    /*
    * 设置默认地址
    * */
    @Override
    public void setDefault(AddressBook addressBook) {
        //默认地址只有一个，先根据用户id查询 有没有默认地址值为1的
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,userId).eq(AddressBook::getIsDefault,1);
        AddressBook one = this.getOne(queryWrapper);
        if (one!=null){
            //有默认地址，将其设置为非默认地址
            log.info("service里面查询默认地址为1的{}",one.toString());
            one.setIsDefault(0);
            log.info("要修改为非默认地址：{}",one.toString());
            this.updateById(one);
        }
        //设置地址为默认地址
        addressBook.setIsDefault(1);
        this.updateById(addressBook);
    }
}
